package com.fingard.dsp.bank.directbank.cmb05;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.cmb04.util.SDKConfig;
import com.fingard.dsp.bank.directbank.cmb04.util.SDKUtil;
import com.fingard.dsp.bank.directbank.cmb05.httpUtil.HttpHelper;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpReceiver;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.google.gson.Gson;
import net.sf.json.JSONArray;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class CMB05Base extends DirectBase implements ITcpClient {
    private static TransIDHelper idHelper = new TransIDHelper(1, 999);

    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.DateTime12Year2Format.format(tmpDateNow) + idHelper.getNextIDStr();
    }

    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }

    protected void writeCommonHeader(StringBuffer sb, CMB05Header header) {
        sb.append("UID=");
        sb.append(bankFront.loginName);
        sb.append("&DATA=");
    }

    protected String signByRSA(ActSetItem actItem, String signData) {
        Map<String, String> data_map = new HashMap<String, String>();
        data_map.put(SDKUtil.param_data, signData);

        Properties properties = new Properties();
        properties.setProperty("acpsdk.signCert.path", actItem.ownKeyStorePath);
        properties.setProperty("acpsdk.signCert.pwd", actItem.ownKeyPassword);
        properties.setProperty("acpsdk.signCert.type", "PKCS12");
        properties.setProperty("acpsdk.validateCert.path", actItem.oppCertFilePath);

        SDKConfig.getConfig().loadPropertiesFromSrc(properties);
        SDKUtil.sign(data_map);
        WriteBankLogLn("签名结果:" + data_map.get(SDKUtil.param_signature));
        return data_map.get(SDKUtil.param_signature);
    }

    protected String[] sendToBank(String p_reqStr) throws Exception {
        return sendToBank(p_reqStr, "", "");
    }
    /**
     * 返回数组：[0]ConnState   [1]报文   [2]retCode  [3]retInfo
     */
    protected String[] sendToBank(String p_reqStr, String transType, String commandCode) throws Exception {
        String[] tmpStrRet = new String[]{"", ""};
        if(StringHelper.hasAnyChar(transType) && StringHelper.hasAnyChar(commandCode)) {
            this.detailSimulateForBank(bankFront.bankCode, transType, commandCode, p_reqStr, tmpStrRet);
            if (tmpStrRet[1].length() > 0) {
                return tmpStrRet;
            }
        }
        WriteBankLogStep2(p_reqStr);

        WebRequest tmpWebRequest = new WebRequest(bankFront.serverURL);
        tmpWebRequest.setLogCode(this.getLogCode());
        tmpWebRequest.setBankFront(this.bankFront);
        tmpWebRequest.setConnectTimeout(10000);
        tmpWebRequest.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        tmpWebRequest.setRequestProperty("connection", "Close");
        WriteBankLogLn("请求的银行地址为：" + bankFront.serverURL);
        //   HashMap hashMap = JSON.parseObject(p_reqStr, HashMap.class);
        tmpStrRet[1] = HttpHelper.doPostForm(bankFront.serverURL, p_reqStr);
        if (tmpStrRet[1].contains("fildat")) {// 回单的报文太长，在后面针对个别字段做日志
        } else {
            WriteBankLogStep3(tmpStrRet[1]);
        }
        if (tmpStrRet[0].length() == 0) {
            int tmpIndex = tmpStrRet[1].indexOf("<");
            if (tmpIndex > 0) {
                tmpStrRet[1] = tmpStrRet[1].substring(tmpIndex);
            }
        }
        return tmpStrRet;
    }

    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            if (p_receiver.readedLength >= 4) {
                p_receiver.contentLength = Integer.parseInt(new String(p_receiver.recBytes, 4, 8, getCharset()));
                p_receiver.contentStartIndex = 4;
            }
        }
    }
}
